package com.luckberry.murder.controller.api;

import com.alibaba.fastjson.JSON;
import com.luckberry.murder.entity.House;
import com.luckberry.murder.service.HouseService;
import com.luckberry.murder.util.JSONUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping("/api")
public class ApiHouseController {

    @Resource
    private HouseService service;

    @ResponseBody
    @RequestMapping(value = "/houses", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getHouses() {
        return JSON.toJSONString(service.getHouses());
    }

    @ResponseBody
    @RequestMapping(value = "/houses/script", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getHousesByScript(int sid) {
        return JSON.toJSONString(service.getHousesByScript(sid));
    }

    @ResponseBody
    @RequestMapping(value = "/house/{hid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.GET)
    public String getHouse(@PathVariable("hid") int hid) {
        return JSON.toJSONString(service.getHouse(hid));
    }

    @ResponseBody
    @RequestMapping(value = "/house", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.POST)
    public String addHouse(@RequestBody String json) {
        return JSONUtil.result(service.addHouse(JSON.parseObject(json, House.class)));
    }

    @ResponseBody
    @RequestMapping(value = "/house/{hid}", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.DELETE)
    public String deleteHouse(@PathVariable("hid") int hid) {
        return JSONUtil.result(service.deleteHouse(hid));
    }

    @ResponseBody
    @RequestMapping(value = "/house", produces = {"application/json;charset=UTF-8"}, method = RequestMethod.PUT)
    public String editHouse(@RequestBody String json) {
        return JSONUtil.result(service.editHouse(JSON.parseObject(json, House.class)));
    }
}
